﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace SpotOffer.Utils
{
    public class RootNav : NavigationPage
    {
        public class RootPage : MultiPage<Page>
        {

            protected override Page CreateDefault(object item)
            {
                Title = "CreateDefault";
                //Not needed for this use of a MultiPage (At least not that I've seen...)
                throw new NotImplementedException();
            }
           
        }

        public  RootPage Root;

        public string RootTitle
        {
            get { return Root.Title; }
            set
            {
                if (value != null)
                {
                    Root.Title = value;
                }
            }
        }

        public RootNav()
            : base(new RootPage())
        {
            Root = CurrentPage as RootPage;
            //Create the default "Splash" page
            Root.Children.Add(new ContentPage()
            {
                Content = new ActivityIndicator()
                {
                    IsRunning = true,
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                }
            });
        }

        public RootNav(Page firstPage, string titl)
            : base(new RootPage())
        {
            Root = CurrentPage as RootPage;
            Root.Title = Title;
            Root.Children.Add(firstPage);
        }

        public async Task SetRoot(Page firstPage, string title)
        {
            await PopToRootAsync();
            Root.CurrentPage = new RootPage();
            Root.Children.Add(firstPage);
            Root.Title = title;
        }

        public async Task SwapRoot<T>() where T : Page, new()
        {
            await PopToRootAsync();

            //Use the MultiPage to display a new page, removing the old to prevent overlapping of views
            Root.Children.RemoveAt(0);
            Root.Children.Add(new T());

            //TODO Add an animation here to make it look like a normal entrance?
        }

        public async Task SwapRoot(Page newPage, string title)
        {
            //Use the MultiPage to display a new page, removing the old to prevent overlapping of views
            Root.Children.Insert(0,newPage);
            Root.Children.RemoveAt(1);
            Root.Title = title;
        }


    }
}
